home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 …ember: Reference Library / Dev.CD Dec 97 RL.toast / What's New / Tool Chest / Testing & Debugging / Virtual User / Examples / Example Libraries / DataUtils.vulib < prev    next >
Encoding:
Text File  |  1997-10-15  |  6.1 KB  |  189 lines  |  [TEXT/MPS ]

  1. #
  2. #    File:        DataUtils.vulib
  3. #
  4. #    Contains:    Contains quick tasks for working with various data. Use Mark menu to
  5. #                reach the desired task. The tasks are commented on what they do.
  6. #
  7. #    Written by:    D Gaxiola
  8. #
  9. #    Copyright:    © 1992 by Apple Computer, Inc., all rights reserved.
  10. #
  11. #    Change History (most recent first):
  12. #
  13. #                  8/4/92    DGG            renamed Car and Cdr to First and Rest
  14. #                 7/24/92    DGG            added Phil Thompson's FuseBigNumbers task
  15. #                                        added IsSubstring task
  16. #                  6/17/92    DGG            creation 
  17. #
  18. #    To Do:
  19. #
  20.  
  21. (************************************************************************************
  22. * Task Abs(number)
  23. *    Will return the absolute value of a given integer.
  24. ************************************************************************************)
  25. task Abs(number := 0)
  26. begin
  27.     if (number < 0)
  28.         return -number;
  29.     else 
  30.         return number;
  31. end;
  32.  
  33. (************************************************************************************
  34. * Task ConcatStrings(stringList)
  35. *    Will take a list of strings and return a single string which is the
  36. *    concatination of the other strings.
  37. ************************************************************************************)
  38. task ConcatStrings(stringList := {})
  39. begin
  40.     newString := "";
  41.     numberStrings := card stringList;
  42.     for i := 1 to numberStrings
  43.         newString := newString + stringList[i];
  44.     return newString;
  45. end;
  46.  
  47. (************************************************************************************
  48. * Task AddToList(theElement, theList, toFront)
  49. *    Will add an element to the beginning or end of a given list, returning the 
  50. *    resultant list.
  51. ************************************************************************************)
  52. task AddToList(theElement := "", theList := {}, toFront := false)
  53. begin
  54.     if toFront
  55.         return insert(theElement, 1, theList);
  56.     else
  57.         return insert(theElement, ((card theList) + 1), theList);
  58. end;
  59.  
  60. (************************************************************************************
  61. * Task First(theList)
  62. *    This is the equivalent of the car operator from the Scheme language.  It will
  63. *    return the first element of a list.
  64. ************************************************************************************)
  65. task First(theList)
  66. begin
  67.     if ((card theList) > 0)
  68.         return theList[1];
  69.     else
  70.         return undefined;
  71. end;
  72.  
  73. (************************************************************************************
  74. * Task Rest(theList)
  75. *    This is the equivalent of the cdr operator from the Scheme language.  It will
  76. *    return a list of all the elements in a given list, except the first.
  77. ************************************************************************************)
  78. task Rest(theList)
  79. begin
  80.     if ((card theList) > 1)
  81.         return remove(1, theList);
  82.     else
  83.         return undefined;
  84. end;
  85.  
  86. (************************************************************************************
  87. * Task IsSubstring(theSubString, theString)
  88. *    Given a string and a substring, this task will check to see if the substring
  89. *    exists within the main string. If it does, it will return the starting location
  90. *    within the main string, otherwise it will return false.
  91. ************************************************************************************)
  92. task IsSubstring(theSubString := "", theString := "")
  93. begin
  94.     subLength := card theSubString;
  95.     mainLength := card theString;
  96.     if (subLength > mainLength)
  97.         return false;
  98.     else if (subLength = mainLength)
  99.         if (theSubString = theString)
  100.             return 1;
  101.         else
  102.             return false;
  103.     else
  104.         for mainCounter := 1 to (mainLength - (subLength - 1))
  105.         begin
  106.             subCounter := 1;
  107.             tempCounter := mainCounter;
  108.             if theString[tempCounter] = theSubString[subCounter]
  109.             begin
  110.                 while theString[tempCounter] = theSubString[subCounter]
  111.                 begin
  112.                     subCounter := subCounter + 1;
  113.                     tempCounter := tempCounter + 1;
  114.                 end;
  115.                 if (subCounter = subLength + 1)
  116.                     return mainCounter;
  117.             end;
  118.         end;
  119.     return false;
  120. end;
  121.  
  122. (************************************************************************************
  123. * Task PopStack(theStack)
  124. *    Given a stack, this task will return a list of two elements, the newly popped 
  125. *    element and the new stack.  Since "theStack" is never changed by PopStack, we 
  126. *    could use it in this manner:
  127. *        myData := PopStack(myStack)[1];
  128. *        myStack := PopStack(myStack)[2];
  129. ************************************************************************************)
  130. task PopStack(theStack := {})
  131. begin
  132.     twoItemList := {};
  133.     twoItemList := AddToList(First(theStack), twoItemList);
  134.     twoItemList := AddToList(Rest(theStack), twoItemList);
  135.     return twoItemList;
  136. end;
  137.  
  138. (************************************************************************************
  139. * Task PushStack(theElement, theStack)
  140. *    Given an element and a "stack" the element will be "pushed" onto the stack.
  141. *    The new stack is returned.
  142. ************************************************************************************)
  143. task PushStack(theElement := "", theStack := {})
  144. begin
  145.     return AddToList(theElement, theStack, true);
  146. end;
  147.  
  148. (************************************************************************************
  149. * Task MakeAssoc(objectList, dataList)
  150. *    Given two lists of equal size, this will build a list of associations between
  151. *    the two.
  152. ************************************************************************************)
  153. task MakeAssoc(objectList := {}, dataList := {})
  154. begin
  155.     if ((card objectList) = (card dataList))
  156.     begin
  157.         listSize := card objectList;
  158.         assocList := {};
  159.         for i := 1 to listSize
  160.             assocList := AddToList({objectlist[i], dataList[i]}, assocList);
  161.         return assocList;
  162.     end;
  163.     else
  164.         return undefined;
  165. end;
  166.  
  167. (************************************************************************************
  168. * Task SortList(theList)
  169. *    Simple bubble sort routine for sorting lists.
  170. ************************************************************************************)
  171. task SortList(theList := {})
  172. begin
  173.     noChanges := false;
  174.     while (not noChanges)
  175.     begin
  176.         noChanges := true;
  177.         for i := 1 to ((card theList) - 1)
  178.             if theList[i] > theList[i+1]
  179.             begin
  180.                 tempMember := theList[i];
  181.                 theList := replace( theList[i+1], i, theList );
  182.                 theList := replace( tempMember, i+1, theList );
  183.                 noChanges := false;
  184.             end;
  185.     end;
  186.     return theList;
  187. end;
  188.  
  189.